package com.yt.generator.controller;

import com.yt.generator.action.config.WebGeneratorConfig;
import com.yt.generator.action.model.GenData;
import com.yt.generator.action.model.GenQo;
import com.yt.generator.factory.DefaultTemplateFactory;
import com.yt.generator.service.TableService;
import com.yt.generator.common.Result;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * @Description ：代码生成
 * @Author ： fcy
 * @Date ： 2019/04/16 10:54
 */
@RestController
@RequestMapping("/code")
@Slf4j
public class CodeGeneratorController {
    @Resource
    private TableService tableService;

    @Value("${spring.datasource.jdbc-url}")
    private String jdbcUrl;

    @Value("${spring.datasource.username}")
    private String username;

    @Value("${spring.datasource.password}")
    private String password;

    @ApiOperation(value = "获取代码生成所需数据",notes = "获取代码生成所需数据")
    @PostMapping("/message")
    public Result<GenData> message() {
        GenData genData = new GenData();
        genData.setTables(tableService.getAllTables());
        genData.setGenQo(DefaultTemplateFactory.getDefaultParams());
        genData.setTemplates(DefaultTemplateFactory.getDefaultTemplates());
        return new Result<>(genData);
    }

    @ApiOperation("生成代码")
    @PostMapping(value = "/generate")
    public Result generate(@RequestBody GenQo genQo) {
        genQo.setUrl(jdbcUrl);
        genQo.setUserName(username);
        genQo.setPassword(password);
        WebGeneratorConfig webGeneratorConfig = new WebGeneratorConfig(genQo);
        webGeneratorConfig.doMpGeneration();
        webGeneratorConfig.doGunsGeneration();
        return new Result();
    }
}
